'use strict';

const ApiError = require('../error/ApiError')
const ApiErrorNames = require('../error/ApiErrorNames')
let response_formatter = async(ctx)=>{
    console.log(ctx.status)
    if(ctx.status===200){
        if(ctx.body){
            ctx.body = {
                status:200,
                message:'success',
                data:JSON.parse(ctx.body)
            }
        }else{
            ctx.body = {
                status:200,
                message:'success',
            }
        }
    }else{
        ctx.body = {
            status:ctx.status,
            message:'success',
            data:ApiErrorNames.getErrorInfo(ctx.status)
        }
    }
}

let url_filter = async (ctx,next)=>{
       try{
        await next()
       }catch(err){
           console.log(err)
        if(err instanceof ApiError ){
            ctx.status = 200;
            ctx.body = {
                code:err.code,
                message:err.message
            }
        }
       }
       response_formatter(ctx);
    }
module.exports  = url_filter;